package com.hjh.gameforum.service;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.mapping.*;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import com.hjh.gameforum.entity.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class TopicIndexService {

    private static final String TOPIC_INDEX = "topics";

    private final ElasticsearchClient elasticsearchClient;

    @Autowired
    public TopicIndexService(ElasticsearchClient elasticsearchClient) {
        this.elasticsearchClient = elasticsearchClient;
    }

    /**
     * 创建话题索引（修正版）
     */
    public boolean createTopicIndex() throws IOException {
        CreateIndexResponse response = elasticsearchClient.indices().create(c -> c
                .index(TOPIC_INDEX)
                .mappings(m -> m
                        .properties("topicId", p -> p.keyword(k -> k))
                        .properties("title", p -> p.text(t -> t.analyzer("ik_max_word")))
                        .properties("content", p -> p.text(t -> t.analyzer("ik_max_word")))
                        .properties("createdAt", p -> p.date(d -> d))
                        .properties("updatedAt", p -> p.date(d -> d))
                        .properties("viewCount", p -> p.integer(i -> i))
                        .properties("status", p -> p.keyword(k -> k))
                        .properties("userId", p -> p.keyword(k -> k))
                        .properties("categoryId", p -> p.keyword(k -> k))
                        .properties("likes", p -> p.integer(i -> i))
                        .properties("imageUrl", p -> p.keyword(k -> k))
                )
        );

        return response.acknowledged();
    }

    /**
     * 或者使用更清晰的构建方式
     */
    public boolean createTopicIndexV2() throws IOException {
        // 先构建映射属性
        TypeMapping mapping = TypeMapping.of(tm -> tm
                .properties("topicId", Property.of(p -> p.keyword(k -> k)))
                .properties("title", Property.of(p -> p.text(t -> t.analyzer("ik_max_word"))))
                .properties("content", Property.of(p -> p.text(t -> t.analyzer("ik_max_word"))))
                .properties("createdAt", Property.of(p -> p.date(d -> d)))
                .properties("updatedAt", Property.of(p -> p.date(d -> d)))
                .properties("viewCount", Property.of(p -> p.integer(i -> i)))
                .properties("status", Property.of(p -> p.keyword(k -> k)))
                .properties("userId", Property.of(p -> p.keyword(k -> k)))
                .properties("categoryId", Property.of(p -> p.keyword(k -> k)))
                .properties("likes", Property.of(p -> p.integer(i -> i)))
                .properties("imageUrl", Property.of(p -> p.keyword(k -> k)))
        );

        CreateIndexResponse response = elasticsearchClient.indices().create(c -> c
                .index(TOPIC_INDEX)
                .mappings(mapping)
        );

        return response.acknowledged();
    }

    /**
     * 删除话题索引
     */
    public boolean deleteTopicIndex() throws IOException {
        DeleteIndexResponse response = elasticsearchClient.indices().delete(d -> d
                .index(TOPIC_INDEX)
        );
        return response.acknowledged();
    }

    /**
     * 检查索引是否存在
     */
    public boolean indexExists() throws IOException {
        return elasticsearchClient.indices().exists(e -> e.index(TOPIC_INDEX)).value();
    }

    /**
     * 索引单个话题
     */
    public String indexTopic(Topic topic) throws IOException {
        return elasticsearchClient.index(i -> i
                .index(TOPIC_INDEX)
                .id(topic.getTopicId().toString())
                .document(topic)
        ).id();
    }

    /**
     * 删除话题文档
     */
    public void deleteTopic(Long topicId) throws IOException {
        elasticsearchClient.delete(d -> d
                .index(TOPIC_INDEX)
                .id(topicId.toString())
        );
    }

    /**
     * 更新话题文档
     */
    public String updateTopic(Topic topic) throws IOException {
        return elasticsearchClient.index(i -> i
                .index(TOPIC_INDEX)
                .id(topic.getTopicId().toString())
                .document(topic)
        ).id();
    }
}